<template>
    <view class="header">
        {{ $t('index.search') }}
    </view>
    <view class="main">
        <view class="search">
            <image class="search-icon" src="../../static/icon/search-icon.png" mode="" />
            <input type="text" @focus="clearHolder(true)" @blur="clearHolder(false)" @confirm="submit" v-model="keyWords"
                :placeholder="isFocus ? '' : $t('index.search.placeholder')">
        </view>
        <view class="search-history">
            <view class="text">{{ $t('index.search.history') }}</view>
            <image @click="showTips" class="lagage" src="../../static/icon/lagage.png" mode="" />
        </view>
        <view class="history-box">
            <template v-if="history.length == 0">
                <view class="empty">
                    {{ $t('index.search.no-history') }}
                </view>
            </template>
            <template v-if="history.length != 0">
                <view class="itemBox">
                    <view class="item" v-for="(item, index) in history" :key="index" @click="submit(item)">
                        {{ item }}
                    </view>
                </view>
            </template>
        </view>
        <view class="search-history stock">
            <view class="text">{{ $t('index.search.stock-list') }}</view>
        </view>
        <view class="stock-box">
            <template v-if="result.length == 0">
                <view class="empty">
                    {{ $t('index.search.no-result') }}
                </view>
            </template>
            <template v-else>
                <view class="grid">
                    <view class="content" v-for="(item, index) in result" :key="index"
                        @click="routerTodetail(item.stockCode)">
                        <view class="first-line">
                            <view class="info">
                                <view class="name">{{ item.stockName }}</view>
                                <view class="code">{{ item.symbol }}</view>
                            </view>
                            <image class="info-more" src="../../static/icon/lagage.png" mode="" />
                        </view>
                        <view class="second-line">
                            <view
                                :class="['dot1', { red: item.rate > 0 && item.rate != 0 }, { green: item.rate < 0 && item.rate != 0 }]">
                                {{ item.regularMarketPrice }}
                            </view>
                            <view
                                :class="['dot2', { red: item.rate > 0 && item.rate != 0 }, { green: item.rate < 0 && item.rate != 0 }]">
                                {{ item.rate }}%
                            </view>
                        </view>
                    </view>
                </view>
            </template>
        </view>
    </view>
    <!-- 弹窗 -->

    <view class="tips" v-if="isTips">
        <view class="out">
            <!-- 遮罩层 -->
            <view class="mask" @click="closeTips"></view>
            <view class="container">
                <view class="box">
                    <view class="tips-title">{{ $t('transaction.confirm-bool') }}</view>
                    <view class="key cancel" @click="closeTips">{{ $t('cancel') }}</view>
                    <view class="key confirm" @click="clearHistory">{{ $t('confirm') }}</view>
                </view>
            </view>
        </view>
    </view>
</template>

<script setup>
import { onMounted, ref, computed } from 'vue';
import { requestApi } from '@/api'
import { onShow } from '@dcloudio/uni-app';
import { setKey, getKey, removeKey } from '@/utils/storage';
/*
    1、綁定輸入框的值
    2、每次搜索后保存值進本地存儲
    3、頁面掛載后通過代碼去請求股票的數據
    4、然後渲染
*/
// 彈窗
const isFocus = ref(false)
const isTips = ref(false)
function clearHolder(bool) {
    isFocus.value = bool
}
function clearHistory() {
    new Promise((resolve, reject) => {
        removeKey('history')
        history.value = []
        result.value = []
        resolve()
        isTips.value = false
    })

}
function showTips() {
    isTips.value = true
}
function closeTips() {
    isTips.value = false
}
const keyWords = ref('')
const history = ref([])
const result = ref([])
const routerTodetail = (code) => {
    uni.navigateTo({
        url: '/pages/market/market-detail?code=' + JSON.stringify(code)
    })
}
// 彈窗
async function submit(key) {
    if (typeof key == 'string') {
        keyWords.value = key
    }
    // 存儲
    if (history.value.indexOf(keyWords.value) == -1) {
        history.value.push(keyWords.value)
        setKey('history', history.value)
    }
    const option = {
        keyWords: keyWords.value,
        region: 'TaiWan',
        interval: '1mo'
    }
    const { data, status } = await requestApi.getStock(option)
    if (status == 0) {
        keyWords.value = ''
        const { list } = data
        result.value = list.map((item) => {
            let { stockName, result } = item
            const res = result.map((ele) => {
                const { regularMarketPrice, rate, symbol } = ele.meta
                let numrate = 100 * rate
                numrate = Number(numrate.toFixed(2))
                return {
                    regularMarketPrice,
                    rate: numrate,
                    symbol: symbol.match(/^[^\.]+/)[0],
                    stockCode: symbol
                }
            })
            const { regularMarketPrice, rate, symbol, stockCode } = res[0]
            return {
                stockName,
                regularMarketPrice,
                rate,
                symbol,
                stockCode
            }
        })
    }
}
onMounted(() => {
    history.value = getKey('history') || []
})
</script>

<style lang="scss">
page {
    background-color: $uni-bg-color-dark;
}

.header {
    background-color: $uni-bg-color-dark;
    height: 88rpx;
    // #ifndef H5
    margin-top: 88rpx;
    // #endif
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0rpx 40rpx;
    color: #fff;
    font-size: 36rpx;
}

.main {
    padding: 0 40rpx 0;

    .search {
        background-color: $uni-bg-color-light-dark;
        margin-left: 25rpx;
        width: 100%;
        height: 64rpx;
        border-radius: 110rpx;
        position: relative;

        .search-icon {
            position: absolute;
            top: 18rpx;
            left: 26rpx;
            width: 28rpx;
            height: 28rpx;
        }

        input {
            color: $uni-text-color-placeholder;

            padding: 11rpx 0 0 77rpx;
            font-size: 28rpx;
        }
    }

    .search-history {
        display: flex;
        align-items: center;
        margin-top: 46rpx;

        .text {
            color: #7E8394;
        }

        image {
            margin-left: auto;
            width: 50rpx;
            height: 50rpx;
        }
    }

    .history-box {
        color: #7E8394;
        height: 250rpx;
        display: flex;

        .empty {
            margin: auto;
        }

        .itemBox {
            color: #fff;
            display: grid;
            margin-top: 20rpx;
            grid-template-columns: repeat(3, minmax(0, 1fr));

            .item {
                width: 296rpx;
            }
        }
    }

    .stock {
        margin-bottom: 20rpx;
    }

    .stock-box {
        display: flex;

        .grid {
            margin-top: 26rpx;
            display: grid;
            grid-column-gap: 20rpx;
            column-gap: 20rpx;
            grid-row-gap: 26rpx;
            row-gap: 26rpx;
            grid-template-columns: repeat(2, minmax(0, 1fr));

        }

        .empty {
            margin: auto;
            color: #7E8394;
        }
    }

    .content {
        background-color: #292A36;
        width: 324rpx;
        height: 197rpx;
        border-radius: 40rpx;

        .first-line {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding-top: 37rpx;
            padding-left: 24rpx;
            padding-right: 26rpx;

            .info {
                .name {
                    color: #fff;
                    font-size: 28rpx;
                }

                .code {
                    color: #7E8394;
                    font-size: 24rpx;
                }
            }


            .info-more {
                width: 48rpx;
                height: 48rpx;
            }
        }

        .second-line {
            display: flex;
            justify-content: space-between;
            padding-left: 24rpx;
            padding-right: 26rpx;
            margin-top: 25rpx;

            .dot1,
            .dot2 {
                color: #7E8394;
                height: 43rpx;
            }
        }

    }
}

.tips {
    position: fixed;
    top: 0rpx;
    left: 0rpx;
    z-index: 999;
    width: 100%;
    height: 100%;

    .mask {
        background-color: rgba(0, 0, 0, .4);
        width: 100%;
        height: 100%;
    }

    .out {
        position: relative;
        width: 100%;
        height: 100%;
    }

    .container {
        width: 525rpx;
        height: 250rpx;
        background-color: #292A36;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        border-radius: 40rpx;
        overflow: hidden;

        .box {
            width: 100%;
            height: 100%;
            position: relative;
            color: #fff;
            display: flex;
            justify-content: space-between;
            align-items: center;

            .close {
                width: 50rpx;
                height: 50rpx;
                position: absolute;
                right: 30rpx;
                top: 33rpx;
                z-index: 999;
            }

            .key {
                width: 168rpx;
                height: 56rpx;
                border-radius: 130rpx;
                text-align: center;
                line-height: 56rpx;
                margin-top: 70rpx;
            }

            .confirm {
                margin-right: 50rpx;
                background-color: #2D7DFC;
            }

            .cancel {
                margin-left: 50rpx;
            }

            .tips-title {
                position: absolute;
                top: 43rpx;
                left: 50%;
                transform: translateX(-50%);
            }

        }
    }
}

.red {
    color: #F04848 !important;
}

.green {
    color: #22A875 !important;
}
</style>